package gui;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import parser.ParserAdd;
import parser.ParserDelete;
import parser.ParserDisplay;
import parser.ParserMark;
import parser.ParserSearch;
import parser.ParserUpdate;
import logger.Logger;
import logic.Display;
import logic.History;
import memory.Memory;


public class Main {

	private static final String CLEAR_SCREEN = "clear screen";
	private static final String MARK ="mark";
	private static final String REDO = "redo";
	private static final String UNDO = "undo";
	private static final String SEARCH = "search";
	private static final String EXIT = "exit";
	private static final String UPDATE = "update";
	private static final String DELETE = "delete";
	private static final String DISPLAY = "display";
	private static final String ADD = "add";


	private static final String INVALID_COMMAND = "Invalid command, please try again";

	private static String command;

	//@author A0097013L
	public static void printWelcomeMessage()throws IOException{
		InputStream in = GuiMain.class.getClassLoader().getResourceAsStream("welcome.txt");

		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		StringBuilder out = new StringBuilder();
		String line;
		while ((line = reader.readLine()) != null) {
			out.append(line);
			out.append("\n");
		}
		System.out.println(out.toString());   //Prints the string content read from input stream
		reader.close();

	}

	//@author A0085082E
	/**
	 * Takes in user command and run methods associated
	 * Request for user to retype command again if invalid
	 * 
	 */
	public static void runCommands(String comd, Memory memory) {

		trimCommand(comd);
		if(command.length() != 0){

			Logger.add("User run command: " + command);
			printCurrentCommand();

			if(command.toLowerCase().startsWith(ADD)){
				trimType(ADD);
				ParserAdd.run(command, memory);
			}else if(command.toLowerCase().startsWith(DISPLAY)){
				trimType(DISPLAY);
				ParserDisplay.run(command, memory);
			}else if(command.toLowerCase().startsWith(DELETE)){
				trimType(DELETE);
				ParserDelete.run(command, memory);
			}else if(command.toLowerCase().startsWith(SEARCH)){
				trimType(SEARCH);
				ParserSearch.run(command, memory);
			}else if(command.toLowerCase().startsWith(UPDATE)){
				trimType(UPDATE);
				ParserUpdate.run(command, memory);
			}else if(command.toLowerCase().startsWith(EXIT)){
				System.exit(0);
			}else if(command.toLowerCase().startsWith(UNDO)){
				History.undo(memory);
			}else if(command.toLowerCase().startsWith(REDO)){
				History.redo(memory);
			}else if(command.toLowerCase().startsWith(MARK)){
				trimType(MARK);
				ParserMark.run(command, memory);
			}else if(command.toLowerCase().startsWith(CLEAR_SCREEN)){
				Display.displayClear();
			}else{
				Display.displayClear();
				System.out.println(INVALID_COMMAND);
				Logger.add("Error: " + INVALID_COMMAND);
			}
		}
	}

	//@author A0085082E
	private static void trimCommand(String comd) {
		command = comd;
		command = command.trim();
	}

	//@author A0085082E
	private static void trimType (String type){
		command = command.substring(type.length());
		command = command.trim();
	}

	//@author A0085082E
	private static void printCurrentCommand(){
		System.out.println(command +"\n");
	}
}
